package com.qfedu.openplateform.manager.controller;

import com.github.pagehelper.PageInfo;
import com.qfedu.openplateform.manager.entity.AjaxMessage;
import com.qfedu.openplateform.manager.entity.ApiMapping;
import com.qfedu.openplateform.manager.entity.TableData;
import com.qfedu.openplateform.manager.service.ApiMappingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/sys/api_mapping")
public class ApiMappingController {

    @Autowired
    private ApiMappingService apiMappingService;

    /**
     * 路由管理----->查询路由列表
     * @param apiMapping
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping( "/table")
    public TableData table(ApiMapping apiMapping, Integer page, Integer limit) {
        PageInfo<ApiMapping> pageInfo = apiMappingService.getApiMappingList(apiMapping, page, limit);
        return new TableData(pageInfo.getTotal(), pageInfo.getList());
    }


    /**
     * 路由管理----->添加路由
     * @param apiMapping
     * @return
     */
    @RequestMapping( "/add")
    public AjaxMessage add(ApiMapping apiMapping) {
        try {
            apiMappingService.addApiMapping(apiMapping);
            return new AjaxMessage(true, "添加成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new AjaxMessage(false, "添加失败");
    }


    /**
     * 路由管理----->删除路由
     * @param ids
     * @return
     */
    @RequestMapping( "/del")
    public AjaxMessage delete(int[] ids) {
        try {
            apiMappingService.deleteApiMapping(ids);
            return new AjaxMessage(true, "删除成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new AjaxMessage(false, "删除失败");
    }


    /**
     * 路由管理----->修改路由
     * @param apiMapping
     * @return
     */
    @RequestMapping( "/update")
    public AjaxMessage update(ApiMapping apiMapping) {
        try {
            apiMappingService.updateApiMapping(apiMapping);
            return new AjaxMessage(true, "修改成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new AjaxMessage(false, "修改失败");
    }





}
